package sq.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import sq.dao.Food;
import sq.dao.FoodType;
import sq.mapper.FoodMapper;
import sq.repository.FoodRepository;
import sq.service.FoodService;
import sq.util.QiniuUploadService;
import sq.vo.BaseRespVO;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


/**
 * sunquan
 * 文件上传下载接口
 */

@Controller
public class FoodController {
    @Autowired
    private FoodService foodService;
    @Autowired
    private FoodMapper foodMapper;

    /**
     * 实现菜品上传
     */
    @RequestMapping(value = "/food/up", method = RequestMethod.POST)
    @ResponseBody
    public BaseRespVO foodUpload(Food food, MultipartFile file, HttpServletRequest request) throws IOException {
        BaseRespVO baseRespVO = new BaseRespVO();
        String updateFlag = foodService.foodUpload(food, file, request);
        if ("1".equals(updateFlag)) {
            baseRespVO.setCode("1");
            baseRespVO.setMsg("文件上传失败");
        }
        else if ("0".equals(updateFlag)) {
            baseRespVO.setCode("0");
            baseRespVO.setMsg("文件上传成功");
        }
        else if ("2".equals(updateFlag)) {
            baseRespVO.setCode("2");
            baseRespVO.setMsg("文件上传失败(请使用正确的图片格式)");
        }
        return baseRespVO;
    }

    /**
     * 实现菜品种类查询
     */
    @RequestMapping(value = "/food/type", method = RequestMethod.POST)
    @ResponseBody
    public BaseRespVO queryFoodType() {
        BaseRespVO baseRespVO = new BaseRespVO();
        ArrayList<FoodType> list = (ArrayList<FoodType>) foodService.findAll();
        if (list.isEmpty()) {
            baseRespVO.setCode("1");
            baseRespVO.setMsg("没有文件查询失败");
        } else {
            baseRespVO.setCode("0");
            baseRespVO.setData(list);
            baseRespVO.setMsg("查询成功");
        }
        return baseRespVO;
    }

    /**
     * 查询所有菜品
     *
     * @return
     */
    @RequestMapping(value = "/food/show/all", method = RequestMethod.POST)
    @ResponseBody
    public BaseRespVO queryFoodTotal() {
        BaseRespVO baseRespVO = new BaseRespVO();
        ArrayList<Food> list = (ArrayList<Food>) foodService.findAllFood();
        if (list.isEmpty()) {
            baseRespVO.setCode("1");
            baseRespVO.setMsg("没有文件查询失败");
        } else {
            baseRespVO.setCode("0");
            baseRespVO.setData(list);
            baseRespVO.setMsg("查询成功");
        }
        return baseRespVO;
    }

    /**
     * 查询指定菜品
     *
     * @return
     */
    @RequestMapping(value = "/food/show/result", method = RequestMethod.POST)
    @ResponseBody
    public BaseRespVO queryFoodBySearchBar(int foodType,String foodName) {
        BaseRespVO baseRespVO = new BaseRespVO();
        ArrayList<Food> list = (ArrayList<Food>) foodService.findSearchFood(foodType,foodName);
        if (list.isEmpty()) {
            baseRespVO.setCode("1");
            baseRespVO.setMsg("没有文件查询失败");
        } else {
            baseRespVO.setCode("0");
            baseRespVO.setData(list);
            baseRespVO.setMsg("查询成功");
        }
        return baseRespVO;
    }

    /**
     * 删除菜品
     *
     * @param foodId
     * @return
     */
    @RequestMapping(value = "/food/delete/{foodId}", method = RequestMethod.POST)
    @ResponseBody
    public BaseRespVO deleteFood(@PathVariable(name = "foodId") Integer foodId) {
        BaseRespVO baseRespVO = new BaseRespVO();
        foodService.deletFood(foodId);
        baseRespVO.setCode("0");
        baseRespVO.setMsg("删除成功");
        return baseRespVO;
    }


    /**
     * 根据id查询菜品
     *
     * @return
     */
    @RequestMapping(value = "/food/show/{foodId}", method = RequestMethod.POST)
    @ResponseBody
    public BaseRespVO<Food> queryFoodById(@PathVariable(name = "foodId") int foodId) {
        BaseRespVO<Food> baseRespVO = new BaseRespVO<Food>();
        Food food = foodService.queryFood(foodId);
        if (food == null) {
            baseRespVO.setCode("1");
            baseRespVO.setMsg("没有文件查询失败");
        } else {
            baseRespVO.setCode("0");
            baseRespVO.setData(food);
            baseRespVO.setMsg("查询成功");
        }
        return baseRespVO;
    }

    /**
     * 更新菜品
     */
    @RequestMapping(value = "/food/update", method = RequestMethod.POST)
    @ResponseBody
    public BaseRespVO<Food> foodUpdate(Food food) {
        BaseRespVO<Food> baseRespVO = new BaseRespVO<Food>();
        foodService.updateFood(food);
        baseRespVO.setCode("0");
        baseRespVO.setMsg("更新成功");
        return baseRespVO;
    }

    /**
     * 上传菜品种类
     */
    @RequestMapping(value = "/type/up", method = RequestMethod.POST)
    @ResponseBody
    public BaseRespVO<FoodType> typeUp(FoodType foodType) {
        BaseRespVO<FoodType> baseRespVO = new BaseRespVO<FoodType>();
        if(foodService.addType(foodType)!=null){
            baseRespVO.setCode("0");
            baseRespVO.setMsg("新增成功");
        }
        else{
            baseRespVO.setCode("1");
            baseRespVO.setMsg("新增失败");
        }

        return baseRespVO;
    }

    /**
     * 分页查询所有菜品
     */
    @RequestMapping(value = "/food/page", method = RequestMethod.POST)
    @ResponseBody
    public BaseRespVO findFoodPage(@RequestParam(value = "pageNum", required = false, defaultValue="1") Integer pageNum,
                                              @RequestParam(value = "pageSize", required = false, defaultValue="5") Integer pageSize,
                                   @RequestParam(value = "type") Integer type,
                                   @RequestParam(value = "foodName") String foodName) {
        BaseRespVO baseRespVO = new BaseRespVO();
        //使用分页插件,核心代码就这一行
        PageHelper.startPage(pageNum, pageSize);
        List<Food> list=null;
        if(foodName==null||foodName==""){
            if(type==0){
                list= foodMapper.findAll();
            }else{
                list= foodMapper.findFoodByType(type);
            }
        }else{
            foodName="%"+foodName+"%";
            if(type==0){
                list= foodMapper.findFoodByName(foodName);
            }else{
                list= foodMapper.findFoodByNameAndType(foodName,type);
            }
        }

        PageInfo<Food> pageInfo = new PageInfo<Food>(list);
        baseRespVO.setCode("0");
        baseRespVO.setData(pageInfo);
        return baseRespVO;
    }
    /**
     * 分页查询指定菜品
     */
    @RequestMapping(value = "/food/page/result", method = RequestMethod.POST)
    @ResponseBody
    public BaseRespVO findFoodPageByName(@RequestParam(value = "pageNum", required = false, defaultValue="1") Integer pageNum,
                                   @RequestParam(value = "pageSize", required = false, defaultValue="5") Integer pageSize,
                                   @RequestParam(value = "type") Integer type,
                                         @RequestParam(value = "foodName") String foodName) {
        BaseRespVO baseRespVO = new BaseRespVO();

        //使用分页插件,核心代码就这一行
        PageHelper.startPage(pageNum, pageSize);
        List<Food> list=null;
        if(foodName==null||foodName==""){
            if(type==0){
                list= foodMapper.findAll();
            }else{
                list= foodMapper.findFoodByType(type);
            }
        }else{
            foodName="%"+foodName+"%";
            if(type==0){
                list= foodMapper.findFoodByName(foodName);
            }else{
                list= foodMapper.findFoodByNameAndType(foodName,type);
            }
        }
        PageInfo<Food> pageInfo = new PageInfo<Food>(list);
        baseRespVO.setCode("0");
        baseRespVO.setData(pageInfo);
        return baseRespVO;
    }

}
